package com.bz.common;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.metadata.OrderItem;

/**
 * Description: 分页数据对象<br>
 */
public class PageInfo<T> implements IPage<T> {

    private static final long serialVersionUID = -8551077538866242945L;

    private List<OrderItem>   orders           = new ArrayList<>();

    private List<T>           records          = Collections.emptyList();

    private long              total            = 0;

    private long              size             = 10;

    private long              current          = 1;

    private boolean           optimizeCountSql = true;

    public PageInfo() {}

    /**
     * Description: 构造分页参数对象<br>
     * 
     * @param current
     *            当前页数
     * @param size
     *            页大小
     */
    public PageInfo(long current, long size) {
        this(current, size, 0);
    }

    /**
     * Description: 构造分页参数对象<br>
     * 
     * @param current
     *            当前页数
     * @param size
     *            页大小
     * @param total
     *            数据总条数
     */
    public PageInfo(long current, long size, long total) {
        this(current, size, total, true);
    }

    /**
     * Description: 构造分页参数对象<br>
     * 
     * @param current
     *            当前页数
     * @param size
     *            页大小
     * @param isSearchCount
     *            是否查询总条数
     */
    public PageInfo(long current, long size, boolean isSearchCount) {
        this(current, size, 0, isSearchCount);
    }

    /**
     * Description: 构造分页参数对象<br>
     * 
     * @param current
     *            当前页数
     * @param size
     *            页大小
     * @param total
     *            数据总条数
     * @param isSearchCount
     *            是否查询总条数
     */
    public PageInfo(long current, long size, long total, boolean isSearchCount) {
        if (current > 1) {
            this.current = current;
        }
        this.size = size;
        this.total = total;
    }

    @Override
    public List<OrderItem> orders() {
        return getOrders();
    }

    @Override
    public List<T> getRecords() {
        return this.records;
    }

    @Override
    public PageInfo<T> setRecords(List<T> records) {
        this.records = records;
        return this;
    }

    @Override
    public long getTotal() {
        return this.total;
    }

    @Override
    public PageInfo<T> setTotal(long total) {
        this.total = total;
        return this;
    }

    @Override
    public long getSize() {
        return this.size;
    }

    @Override
    public PageInfo<T> setSize(long size) {
        this.size = size;
        return this;
    }

    @Override
    public long getCurrent() {
        return this.current;
    }

    @Override
    public PageInfo<T> setCurrent(long current) {
        this.current = current;
        return this;
    }

    @Override
    public boolean optimizeCountSql() {
        return optimizeCountSql;
    }

    public List<OrderItem> getOrders() {
        return orders;
    }

    public void setOrders(List<OrderItem> orders) {
        this.orders = orders;
    }

    @Override
    public long getPages() {
        if (getSize() == 0) {
            return 0L;
        }
        long pages = getTotal() / getSize();
        if (getTotal() % getSize() != 0) {
            pages++;
        }
        return pages;
    }
}
